/**
 *	@author  >> Justin Windle
 *	@link	 >> soulwire.co.uk
 * 
 * 
 */

package dk
{
	import dk.com.soulwire.geom.ColourMatrix;
	import dk.com.soulwire.media.MotionTracker;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.ColorMatrixFilter;
	import flash.media.Camera;
	import flash.media.Video;
	
	import mx.controls.TextInput;

	public class DetectMotionView extends Sprite
	{
		
		/*
		========================================================
		| Private Variables                         | Data Type  
		========================================================
		*/
		
		private var _motionTracker:					MotionTracker;
		
		private var _target:						Shape;
		private var _bounds:						Shape;
		private var _output:						Bitmap;
		private var _source:						Bitmap;
		private var _video:							BitmapData;
		private var _matrix:						ColourMatrix;
		
		//added by Duykham on 200905230749
		public var motionAreaX:						Number;
		public var motionAreaY:						Number;
		public var motionAreaW:						Number;
		public var motionAreaH:						Number;
		public var _trackingResult:					TextInput;
		//end adding
		
		private var trackingVid:					Video;
		/*
		========================================================
		| Constructor
		========================================================
		*/
		
		public function DetectMotionView(cam:Camera) 
		{
			trace(this+"Constructed!");
			/*** SETUP ***/
			
			/* var cameraW:int = 400;
			var cameraH:int = 300;
			var cameraF:int = 24; */
			
			var cameraW:int = cam.width;
			var cameraH:int = cam.height;
			var cameraF:int = cam.fps; 
			
			trace(cameraH);
			trace(cameraW);
			trace(cameraF);

			// Create the camera
			/* var newcam:Camera = Camera.getCamera();
			newcam.setMode( cameraW, cameraH, cameraF ); */
			
			// Create a video
			trackingVid = new Video(cameraW, cameraH);
			trace("trackingvid.width: "+trackingVid.width);
			 /* trackingVid.width = 320;
			trackingVid.height = 240; 
			trace("vid.width: "+trackingVid.width); */
			
			trackingVid.attachCamera(cam);
			
			
			// Create the Motion Tracker
			_motionTracker = new MotionTracker( trackingVid );
			
			// We flip the input as we want a mirror image
			//_motionTracker.flipInput = true;
			
			
			/*** Create a few things to help us visualise what the MotionTracker is doing... ***/
			
			
			_matrix = new ColourMatrix();
			_matrix.brightness = _motionTracker.brightness;
			_matrix.contrast = _motionTracker.contrast;
			
			// Display the camera input with the same filters (minus the blur) as the MotionTracker is using
			_video = new BitmapData( cameraW, cameraH, false, 0 );
			_source = new Bitmap( _video );
			_source.scaleX = -1;
			_source.x = cameraW;
			_source.filters = [ new ColorMatrixFilter( _matrix.getMatrix() ) ];
			//addChild( _source );
			
			// Show the image the MotionTracker is processing and using to track
			_output = new Bitmap( _motionTracker.trackingImage );
			//_output.x = cameraW;
			addChild( _output );
			
			
			// A shape to represent the tracking point
			_target = new Shape();
			_target.graphics.lineStyle( 0, 0xFFFFFF );
			_target.graphics.drawCircle( 0, 0, 10 );
			//addChild( _target );
			
			// A box to represent the activity area
			_bounds = new Shape();
			_bounds.x = _output.x;
			_bounds.y = _output.y;
			addChild( _bounds );
			
			// Configure the UI
			/* blurStep.addEventListener( Event.CHANGE, setValues );
			brightnessStep.addEventListener( Event.CHANGE, setValues );
			contrastStep.addEventListener( Event.CHANGE, setValues );
			areaStep.addEventListener( Event.CHANGE, setValues ); */
			
			//by Duykham
			_trackingResult = new TextInput();
			_trackingResult.x = _source.x;
			_trackingResult.y = _source.height;
			_trackingResult.width = _source.width;
			addChild( _trackingResult );
			
			// Get going!
			addEventListener( Event.ENTER_FRAME, track );
		}
		
		/*
		========================================================
		| Event Handlers
		========================================================
		*/
		
		private function track( e:Event ):void
		{
			// Tell the MotionTracker to update itself
			_motionTracker.track();
			
			// Move the target with some easing
			_target.x += ((_motionTracker.x + _bounds.x) - _target.x) / 10;
			_target.y += ((_motionTracker.y + _bounds.y) - _target.y) / 10;
			
			_video.draw( _motionTracker.input );
			
			// If there is enough movement (see the MotionTracker's minArea property) then continue
			if ( !_motionTracker.hasMovement ) return;
			
			// Draw the motion bounds so we can see what the MotionTracker is doing
			_bounds.graphics.clear();
			_bounds.graphics.lineStyle( 0, 0xFFFFFF );
			_bounds.graphics.drawRect( _motionTracker.motionArea.x,
									   _motionTracker.motionArea.y,
									   _motionTracker.motionArea.width,
									   _motionTracker.motionArea.height
										);
			
										
			//added by Duykham on 200905230750
			motionAreaX = _motionTracker.motionArea.x;
			motionAreaY = _motionTracker.motionArea.y;
			motionAreaW = _motionTracker.motionArea.width;
			motionAreaH = _motionTracker.motionArea.height;
			_trackingResult.text = 	' X:'+motionAreaX+
									' Y:'+motionAreaY+
									' W:'+motionAreaW+
									' H:'+motionAreaH;
			//end adding
		}
		
		private function setValues( e:Event ):void
		{
			/* switch( e.currentTarget )
			{
				case blurStep :
					_motionTracker.blur = blurStep.value;
				break;
				
				case brightnessStep :
					_motionTracker.brightness = _matrix.brightness = brightnessStep.value;
				break;
				
				case contrastStep :
					_motionTracker.contrast = _matrix.contrast = contrastStep.value;
				break;
				
				case areaStep :
					_motionTracker.minArea = areaStep.value;
				break;
			} */
			
			//added by Duykham
			_motionTracker.blur = 1;
			_motionTracker.brightness = 10;
			_motionTracker.contrast = 10;
			_motionTracker.minArea = 10;
			
			_source.filters = [ new ColorMatrixFilter( _matrix.getMatrix() ) ];
		}
		
	}
	
}
